package echecs;

import outil.Predicat;
import outil.Et;
import outil.Ou;
import static echecs.Pieces.Piece.*;

public class PredicatBase extends ActionBase {

    static Predicat ArrOccup_PiecesCoulOpp;
    static Predicat ArrVideOu_ArrOccupEtPiecesCoulOpp;
    static Predicat ArrOccupEtPiecesCoulOpp;
    static Predicat existeDirSuiv,  estPionDep,  estFinDirPion,  estFouTourDame;
    static Predicat estPion,  estRoiOuCavalier,  ExamineEchecs,  PieceCouleurQuiJoue;
    static Predicat ArrVide,  PieceCouleurEtudiee,  existeCaseSuiv;
    Predicat[] PieceType = {estFouTourDame, estPion, estRoiOuCavalier};

    public PredicatBase() {

        ArrOccup_PiecesCoulOpp = Et.instance(estCaseArrOccup, PieceCouleurOpp);
        ArrOccupEtPiecesCoulOpp = Et.instance(estCaseArrOccup, PieceCouleurOpp);
        existeDirSuiv = new Predicat() {

            @Override
            public boolean eval() {
                return pIndexDir < pPieceDep.getDeplacement().length;
            }
        };
        estPionDep = new Predicat() {

            @Override
            public boolean eval() {
                return Case.valeur(pIndexDep).estARangee2() && pPieceDep.getCouleur() == Couleur.BLANC ||
                        Case.valeur(pIndexDep).estARangee7() && pPieceDep.getCouleur() == Couleur.NOIR;
            }
        };
        estFinDirPion = new Predicat() {

            public boolean eval() {
                return pIndexDir <= 2;
            }
        };
        estFouTourDame = new Predicat() {

            @Override
            public boolean eval() {
                assert pPieceDep != null;
                return pPieceDep.equals(FOU_BLANC) || pPieceDep.equals(FOU_NOIR) ||
                        pPieceDep.equals(TOUR_BLANCHE) || pPieceDep.equals(TOUR_NOIRE) ||
                        pPieceDep.equals(DAME_BLANCHE) || pPieceDep.equals(DAME_NOIRE);
//                return pPieceDep.estFouTourOuDame();
            }
        };
        estPion = new Predicat() {

            @Override
            public boolean eval() {
                return pPieceDep.equals(PION_BLANC) || pPieceDep.equals(PION_NOIR);
            }
        };
        estRoiOuCavalier = new Predicat() {

            public boolean eval() {
                return pPieceDep.equals(ROI_BLANC) || pPieceDep.equals(ROI_NOIR) ||
                        pPieceDep.equals(CAVALIER_BLANC) || pPieceDep.equals(CAVALIER_NOIR);
//                return !pPieceDep.estFouTourOuDame() && !estPion.eval();
            }
        };
        ExamineEchecs = new Predicat() {

            @Override
            public boolean eval() {
                return _examineEchecs;
            }
        };
        PieceCouleurQuiJoue = new Predicat() {

            @Override
            public boolean eval() {
                return pPieceDep != null && pPieceDep.getCouleur() == _couleur;
            }
        };
        ArrVide = new Predicat() {

            @Override
            public boolean eval() {
                return ECHIQUIER[TO_ECHIQUIER[pIndexDep] +
                        pDist * pPieceDep.getDeplacement()[pIndexDir]] != -1 && PIECES[ECHIQUIER[TO_ECHIQUIER[pIndexDep] +
                        pDist * pPieceDep.getDeplacement()[pIndexDir]]] == null;
            }
        };
        ArrVideOu_ArrOccupEtPiecesCoulOpp = Ou.instance(ArrVide, ArrOccup_PiecesCoulOpp);
        PieceCouleurEtudiee = new Predicat() {

            @Override
            public boolean eval() {
                pPieceDep = PIECES[pIndexDep];
                return pPieceDep != null && pPieceDep.getCouleur() == _couleur;
            }
        };
        existeCaseSuiv = new Predicat() {

            @Override
            public boolean eval() {
                return pIndexDep < NB_COLONNES * NB_RANGEES;
            }
        };
    }
    Predicat estCaseArrOccup = new Predicat() {

        @Override
        public boolean eval() {
            return ECHIQUIER[TO_ECHIQUIER[pIndexDep] +
                    pDist * pPieceDep.getDeplacement()[pIndexDir]] != -1 && PIECES[ECHIQUIER[TO_ECHIQUIER[pIndexDep] +
                    pDist * pPieceDep.getDeplacement()[pIndexDir]]] != null;
        }
    };
    Predicat PieceCouleurOpp = new Predicat() {

        @Override
        public boolean eval() {
            return pPieceDep.getCouleur() != PIECES[ECHIQUIER[TO_ECHIQUIER[pIndexDep] +
                    pDist * pPieceDep.getDeplacement()[pIndexDir]]].getCouleur();
        }
    };
}


